package com.ruoyi.ring.service.impl;

import java.util.List;
import java.util.stream.Stream;

import cn.hutool.core.convert.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.bean.Builder;
import com.ruoyi.ring.domain.RingHeartRate;
import com.ruoyi.ring.domain.RingHeartRateInfo;
import com.ruoyi.ring.domain.RingHrvInfo;
import com.ruoyi.ring.domain.dto.RingHrvDTO;
import com.ruoyi.ring.domain.vo.RingHeartRateVO;
import com.ruoyi.ring.domain.vo.RingHrvVO;
import com.ruoyi.ring.mapper.RingHrvInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.ring.mapper.RingHrvMapper;
import com.ruoyi.ring.domain.RingHrv;
import com.ruoyi.ring.service.IRingHrvService;

import javax.annotation.Resource;

/**
 * 心率变异性Service业务层处理
 * 
 * @author liuwp
 * @date 2025-08-22
 */
@Service
public class RingHrvServiceImpl implements IRingHrvService 
{
    @Resource
    private RingHrvMapper ringHrvMapper;

    @Resource
    private RingHrvInfoMapper ringHrvInfoMapper;

    /**
     * 查询心率变异性
     * 
     * @param id 心率变异性主键
     * @return 心率变异性
     */
    @Override
    public RingHrv selectRingHrvById(Long id)
    {
        return ringHrvMapper.selectRingHrvById(id);
    }

    /**
     * 查询心率变异性列表
     * 
     * @param ringHrv 心率变异性
     * @return 心率变异性
     */
    @Override
    public List<RingHrv> selectRingHrvList(RingHrv ringHrv)
    {
        return ringHrvMapper.selectRingHrvList(ringHrv);
    }

    /**
     * 根据业务主键查询一条记录：心率变异性
     *
     * @param ringHrv 心率变异性
     * @return 心率变异性
     */
    @Override
    public RingHrv selectOneRingHrv(RingHrv ringHrv)
    {
        return ringHrvMapper.selectOneRingHrv(ringHrv);
    }

    /**
     * 新增心率变异性
     * 
     * @param ringHrv 心率变异性
     * @return 结果
     */
    @Override
    public int insertRingHrv(RingHrv ringHrv)
    {
        ringHrv.setCreateTime(DateUtils.getNowDate());
        return ringHrvMapper.insertRingHrv(ringHrv);
    }

    /**
     * 修改心率变异性
     * 
     * @param ringHrv 心率变异性
     * @return 结果
     */
    @Override
    public int updateRingHrv(RingHrv ringHrv)
    {
        ringHrv.setUpdateTime(DateUtils.getNowDate());
        return ringHrvMapper.updateRingHrv(ringHrv);
    }

    @Override
    public int updateRingHrvCondition(RingHrv ringHrv, RingHrv ringHrvCondition)
    {
                ringHrv.setUpdateTime(DateUtils.getNowDate());
        return ringHrvMapper.updateRingHrvCondition(ringHrv, ringHrvCondition);
    }

    /**
     * 批量删除心率变异性
     * 
     * @param ids 需要删除的心率变异性主键
     * @return 结果
     */
    @Override
    public int deleteRingHrvByIds(Long[] ids)
    {
        return ringHrvMapper.deleteRingHrvByIds(ids);
    }

    /**
     * 删除心率变异性信息
     * 
     * @param id 心率变异性主键
     * @return 结果
     */
    @Override
    public int deleteRingHrvById(Long id)
    {
        return ringHrvMapper.deleteRingHrvById(id);
    }

    /**
     * 条件删除心率变异性
     *
     * @param ringHrv 心率变异性
     * @return 结果
     */
    @Override
    public int deleteRingHrvByCondition(RingHrv ringHrv)
    {
        return ringHrvMapper.deleteRingHrvByCondition(ringHrv);
    }

    @Override
    public RingHrvVO queryRingHrv(RingHrvDTO ringHrvDTO) {
        RingHrvVO ringHrvVO = new RingHrvVO();
        RingHrv ringHrv = ringHrvMapper.queryOneRingHrv(ringHrvDTO);
        if(ringHrv != null){
            ringHrvVO = Convert.convert(RingHrvVO.class,ringHrv);
            RingHrvInfo ringHrvInfo = ringHrvInfoMapper.selectOneRingHrvInfo(Builder.of(RingHrvInfo::new).with(RingHrvInfo::setHistoryDay, ringHrvVO.getDailyValue()).build());
            if(ringHrvInfo != null){
                String[] hrvs = ringHrvInfo.getHrvList().split(",");//转成String数组
                int[] hrv = Stream.of(hrvs).mapToInt(Integer::parseInt).toArray();//转成int数组
                ringHrvVO.setRealHrvList(hrv);
            }
        }
        return ringHrvVO;
    }

    @Override
    public List<RingHrv> queryRingHrvList(RingHrvDTO ringHrvDTO) {
        return ringHrvMapper.queryRingHrvList(ringHrvDTO);
    }

}
